{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "66b93b64",
   "metadata": {},
   "source": [
    "\n",
    "------------\n",
    "* 时间：2022-10-31  /  week11\n",
    "* 课程：Python语言\n",
    "* 学习记录人：陈泽鑫\n",
    "------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e9544f7",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "* 元组、集合和字典的基本概念和常见的方法\n",
    "\n",
    "> 1. 元组\n",
    "> 2. 集合\n",
    "> 3. 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99bdd709",
   "metadata": {},
   "source": [
    "## 1. 元组（tuple）\n",
    "> 1. 概念\n",
    ">> 1. 具有和序列数据相同的运算规则：取值和切片\n",
    ">> 2. 表征：（12345，54321，‘hello’），用圆括号/小括号 括起来的，中间仍然是用括号隔开的数据形态\n",
    "> 2. 性质：不可变性,没有什么增删改的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e140ad46",
   "metadata": {},
   "outputs": [],
   "source": [
    "product = (\"Head First Python\",88) # 创建了一个元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e98ecdbd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Head First Python', 88) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(product,type(product))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "acfb99f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例：\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e8ed3f99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=31, tm_hour=10, tm_min=20, tm_sec=31, tm_wday=0, tm_yday=304, tm_isdst=0)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b4f0a42c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function localtime in module time:\n",
      "\n",
      "localtime(...)\n",
      "    localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n",
      "                              tm_sec,tm_wday,tm_yday,tm_isdst)\n",
      "    \n",
      "    Convert seconds since the Epoch to a time tuple expressing local time.\n",
      "    When 'seconds' is not passed in, convert the current time instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time.localtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5c0ef04f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1800001, 1800002, 1800003)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_seq = 1800001,1800002,1800003\n",
    "name_seq"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a8eb984",
   "metadata": {},
   "source": [
    "# 2. 集合（Sets）\n",
    "> 1. 集合的概念\n",
    ">> 1. 没有重复元素的一个数据结构\n",
    ">> 2. 形态：{'apple','orange','pear','apple','orange','banana'}\n",
    "> 2. 集合的运算\n",
    ">> 1. 交集运算：&，intersection\n",
    ">> 2. 并集运算：|,union"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9a7e9068",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'orange', 'banana', 'pear', 'apple'} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple', 'orange', 'pear', 'apple', 'orange', 'banana'}\n",
    "print(basket,type(basket))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b4c15034",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {1800001,1800002,1800003,1800004}\n",
    "合唱团 = {1800008,1800003,1800009,1800011}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "71f7a090",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3991d61e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(合唱团)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "98eab580",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 交集运算和并集运算的实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "18530979",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800003}\n",
      "{1800003}\n"
     ]
    }
   ],
   "source": [
    "# 交集\n",
    "print(舞蹈社 & 合唱团)\n",
    "print(舞蹈社.intersection(合唱团))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0726beb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001, 1800002, 1800003, 1800004, 1800008, 1800009, 1800011}\n",
      "7\n",
      "{1800001, 1800002, 1800003, 1800004, 1800008, 1800009, 1800011}\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "# 并集：统计数量\n",
    "print(舞蹈社 | 合唱团)\n",
    "print(len(舞蹈社 | 合唱团))\n",
    "\n",
    "print(舞蹈社.union(合唱团))\n",
    "print(len(舞蹈社.union(合唱团)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8e1af3e",
   "metadata": {},
   "source": [
    "# 3.字典 (dict)\n",
    "> 1. 基本的概念\n",
    ">> 1. mapping types :映射类型\n",
    ">> 2. key:values,键：值 对\n",
    ">> 3. 字典的性质\n",
    ">>> 1. 无序性\n",
    ">>> 2. key不可变性\n",
    ">>> 3. key的唯一性\n",
    "> 2. 字典的创建\n",
    "\n",
    "> 3. 常用的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "09d4c226",
   "metadata": {},
   "outputs": [],
   "source": [
    "...\n",
    "\n",
    "product_list = [\n",
    "     [\"蜜桃四季春\",8],\n",
    "    [\"雪顶咖啡\",9],\n",
    "    [\"柠檬水\",6],\n",
    "    [\"芝士奶盖\",10],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "15a20165",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的创建\n",
    "product_dict ={\n",
    "    \"蜜桃四季春\":8,\n",
    "    \"雪顶咖啡\":9,\n",
    "    \"柠檬水\":6,\n",
    "    \"芝士奶盖\":10\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c8a66609",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'蜜桃四季春': 8, '雪顶咖啡': 9, '柠檬水': 6, '芝士奶盖': 10} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(product_dict,type(product_dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "72b18da1",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [26]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mproduct_dict\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "product_dict[0] # 字典是无序的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "03bb3923",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product_dict[\"蜜桃四季春\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6d799756",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_info = {\n",
    "    \"hanlinfeng\":[\"linfeng\",\"IT\",\"广州\",\"广州南方学院\",\"通讯录\",\"朋友圈\"]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "80a2de35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['linfeng', 'IT', '广州', '广州南方学院', '通讯录', '朋友圈']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info[\"hanlinfeng\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "248a2091",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 作业：字典  做表格\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
